package com.nstar.sinaweibo.model
{
	/**
	
优酷
http://v.youku.com/v_playlist/f4174532o1p15.html
http://v.youku.com/v_show/id_XMjA1OTkyODEy.html
http://player.youku.com/player.php/sid/XMjI1NzI5MDU2/v.swf

新浪视频
http://video.sina.com.cn/v/b/37844868-1337496720.html
http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=37844868_1337496720/s.swf

土豆网
http://www.tudou.com/programs/view/QiNdfBTQ-0o/
http://www.tudou.com/v/QiNdfBTQ-0o/v.swf
 
http://www.tudou.com/playlist/playindex.do?lid=10773620
http://www.tudou.com/l/f1MEM6LlTQ0 

酷6网(X)
http://player.ku6.com/refer/_mD7bVGb82zPQ8AM/v.swf

56视频(X)
http://player.56.com/v_NTY3MDYyMTA.swf

6间房(X)
http://6.cn/p/unpmntD_fYc40RnZt13/5Q.swf	
	
	
	*/
	public class VideoParser
	{
		public static const SRC_YOUKU	:String = "youku";
		public static const SRC_SINA	:String = "sina";
		public static const SRC_TUDOU	:String = "tudou";
		public static const SRC_KU6		:String = "ku6";
		public static const SRC_56		:String = "56";
		public static const SRC_6		:String = "6";
		
		
		
		private static const URL_YOUKU	:String = "v.youku.com";
		private static const URL_SINA	:String = "video.sina.com.cn";
		private static const URL_TUDOU	:String = "www.tudou.com";
		private static const URL_KU6	:String = "v.ku6.com";
		private static const URL_56		:String = "player.56.com";
		private static const URL_6		:String = "v.6.cn";
		
		
		//优酷视频
		private static const YOUKU:RegExp = /http:\/\/v.youku.com\/v_show\/id_(.*?).html/;
		private static const YOUKU_PLAYER:String = "http://player.youku.com/player.php/sid/\$1/v.swf";
		//新浪视频
		private static const SINA:RegExp = /http:\/\/video.sina.com.cn\/v\/b\/(.*?).html/;
		private static const SINA_PLAYER:String = "http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=\$1/s.swf";
		//土豆视频
		private static const TUDOU:RegExp = /http:\/\/www.tudou.com\/programs\/view\/(.*?)\//;
		private static const TUDOU_PLAYER:String = "http://www.tudou.com/v/\$1\/v.swf";
		//酷6视频
		private static const KU6:RegExp = /http:\/\/v.ku6.com\/show\/(.*?).html/;
		private static const KU6_PLAYER:String = "http://player.ku6.com/refer/\$1/v.swf";
		//http://player.ku6cdn.com/default/out/pv201101171815.swf#?ver=108&vid=ee1k5YYWp1QgiokQ&type=v&referer=

		//56视频
		private static const V56:RegExp = /http:\/\/www.56.com\/(.*?)\/(.*?).html/;
		private static const V56_PLAYER:String = "http://player.56.com/\$2.swf";
		
		public function VideoParser()
		{
			
		}
		
		//是否为视频链接，简单的URL判断
		public static function isVideo(url:String){
			return (url.indexOf(URL_YOUKU)!=-1 || url.indexOf(URL_SINA)!=-1 || 
					url.indexOf(URL_TUDOU)!=-1 || url.indexOf(URL_KU6)!=-1);
		}
		
		//判断视频来源
		public static function parseSource(url:String):String{
			if(url.indexOf(URL_YOUKU)!=-1){
				return SRC_YOUKU;
			}else
			if(url.indexOf(URL_SINA)!=-1){
				return SRC_SINA;
			}else
			if(url.indexOf(URL_TUDOU)!=-1){
				return SRC_TUDOU;
			}else
			if(url.indexOf(URL_KU6)!=-1){
				return SRC_KU6;
			}else
			if(url.indexOf(URL_56)!=-1){
				return SRC_56;
			}else
			if(url.indexOf(URL_6)!=-1){
				return SRC_6;
			}else{
				return null;
			}
		}
		
		//取标题
		public static function parseTitle(html:String):String{
			var titleStr:String = "";
			var s1:String = "<title>";
			var p1:int = html.indexOf(s1)+s1.length;
			if(p1!=-1){
				var p2:int = html.indexOf("</title>",p1);
				if(p1!=-10){
					titleStr = html.substr(p1,p2-p1);//标题
				}
			}
			
			//标题太长的话截前30个字符
			if(titleStr.length>30){
				titleStr = titleStr.substr(0,30)+"...";
			}
			return titleStr;
		}
		
		//解析播放器的路径,url为原始播放路径，html为原始视频页面的HTML内容，要从中解析播放器的路径
		public static function parsePlayerUrl(url:String,html:String=null):String{
			var src:String = parseSource(url);//解析视频类型，不同的视频html内容不同
			switch(src){
				case SRC_YOUKU:
					return parseYouku(url,html,"http://player.youku.com","\"");
				case SRC_SINA:
					return parseSina(url,html,"http://you.video.sina.com.cn","',");
				case SRC_TUDOU:
					return parseTudou(url,html,"lid_code = '","'");
				case SRC_KU6:
					return parseKu6(url,html,"http://player.ku6.com","\"");
				case SRC_56:
					return parse56(url);
				case SRC_6:
					return parseKu6(url,html,"http://6.cn/p","&quot;");
			}
			return null;
		}
		
		
		
		//根据url和html解析视频播放器地址
		//fromStart : 是否从start参数指定的字符串开始
		private static function parseNormal(url:String,html:String,
			start:String,end:String,fromStart:Boolean = true):String{
			var result:String ;
			var p1:int = html.indexOf(start);
			if(fromStart){
				p1 = html.indexOf(start);
			}else{
				p1 = html.indexOf(start) + start.length;
			}
			
			if(p1!=-1){
				var p2:int = html.indexOf(end,p1);
				if(p2!=-1){
					result = html.substr(p1,p2-p1);
				}
			}
			return result;
		}		

		//优酷视频
		private static function parseYouku(url:String,html:String,start:String,end:String):String{
			return parseNormal(url,html,start,end);
			//return url.replace(YOUKU,YOUKU_PLAYER);
		}

		//新浪视频
		private static function parseSina(url:String,html:String,start:String,end:String):String{
			return parseNormal(url,html,start,end);
			//return url.replace(SINA,SINA_PLAYER);
		}

		
		//土豆视频
		private static function parseTudou(url:String,html:String,start:String,end:String):String{
			var lidCode:String = parseNormal(url,html,start,end,false);
			return "http://www.tudou.com/l/"+lidCode;
		}
		
		//酷6网
		private static function parseKu6(url:String,html:String,start:String,end:String):String{
			return parseNormal(url,html,start,end);
			//return url.replace(KU6,KU6_PLAYER);
		}
		
		//56视频
		private static function parse56(url:String):String{
			return url.replace(V56,V56_PLAYER);
		}
		
		//6间房
		private static function parse6(url:String,html:String,start:String,end:String):String{
			return parseNormal(url,html,start,end);
		}
		
	}
}